﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.ExceptionHandler;

public partial class Stock_LotesAdmin : GenericPage
{
    Lotes loteActual = new Lotes();
    List<Lotes> listaLotes= new List<Lotes>();
    
    private ExceptionConstructor exNew = new ExceptionConstructor();

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                Mostrar();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }
    #endregion

    #region gvLotes_RowDataBound
    /// <Summary>
    /// Boundeo de la grilla
    /// CBendayan 23/03/2010 
    /// </Summary>
    /// <returns></returns>
    protected void gvLotes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            Label label;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                label = (Label)e.Row.Cells[1].FindControl("FechaVto");
                if (label.Text != "")
                {
                    DateTime dFecha = Convert.ToDateTime(label.Text);
                    if (dFecha == FechaNula)
                    {
                        label.Text = "";
                    }
                }
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvLotes_PageIndexChanging
    protected void gvLotes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvLotes.PageIndex = e.NewPageIndex;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvLotes_SelectedIndexChanged
    protected void gvLotes_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            listaLotes = (List<Lotes>)Session["listaLotes"];
            loteActual = new Lotes();
            string iLote = gvLotes.SelectedDataKey.Value.ToString();
            foreach (Lotes lote in listaLotes)
            {
                if (iLote == lote.Lote)
                {
                    loteActual = lote;
                    break;
                }
            }
            if (loteActual.Lote != "0")
            {
                BMLotes bmLotes = new BMLotes(Connection);
                loteActual = bmLotes.BMLotesGetOne(loteActual);
                Session["loteActual"] = loteActual;
                Session["Accion"] = 2;
                Response.Redirect("LotesGestion.aspx", false);
            }
            else
            {
                /// mensaje Articulo sin lote abierto
                mpeLote.Show();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region btnNuevo_Click
    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        try
        {
            Session["Accion"] = 1;
            Response.Redirect("LotesGestion.aspx", false);
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion

    #region Métodos

    #region CargarGrilla
    private void CargarGrilla()
    {
        try
        {
            listaLotes = (List<Lotes>)Session["listaLotes"];
            gvLotes.DataSource = listaLotes;
            gvLotes.DataBind();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region Mostrar
    private void Mostrar()
    {
        try
        {
            Stock stock = (Stock)Session["Stock"];
            
            loteActual.Id_Rubro = stock.Id_Rubro;
            loteActual.Id_SubRubro = stock.Id_SubRubro;
            loteActual.Id_Articulo = stock.Id_Articulo;
            loteActual.Articulo = stock.Articulo;
            Session["loteActual"] = loteActual;

            BMRubros bmRubro = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            rubro.Id_Rubro = loteActual.Id_Rubro;
            rubro = bmRubro.BMRubrosSearch(rubro)[0];
            lblRubro.Text = "Rubro: " + rubro.Id_Rubro.ToString();
            txtRubro.Text = rubro.Rubro;

            BMSubRubros bmSubRubro = new BMSubRubros(Connection);
            SubRubros subRubro = new SubRubros();
            subRubro.Id_Rubro = loteActual.Id_Rubro;
            subRubro.Id_SubRubro = loteActual.Id_SubRubro;
            subRubro = bmSubRubro.BMSubRubrosSearch(subRubro)[0];
            lblSubRubro.Text = "SubRubro: " + subRubro.Id_SubRubro.ToString();
            txtSubRubro.Text = subRubro.SubRubro;

            txtCodigo.Text = loteActual.Id_Articulo.ToString();
            txtArticulo.Text = loteActual.Articulo;

            loteActual.Estado = 1; // Solo Muestra Lotes abiertos

            listaLotes = new List<Lotes>();
            BMLotes bmLotes = new BMLotes(Connection);
            listaLotes = bmLotes.BMLotesSearch(loteActual);
            Session["listaLotes"] = listaLotes;
            gvLotes.DataSource = listaLotes;
            gvLotes.DataBind();

            CalcularSaldos();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region CalcularSaldos
    private void CalcularSaldos()
    {
        try
        {
            Stock stock = (Stock)Session["Stock"];
            listaLotes = (List<Lotes>)Session["listaLotes"];

            decimal dTLotes = 0;
            foreach (Lotes lote in listaLotes)
            {
                dTLotes += lote.Saldo;
            }
            decimal dSinAsignar = stock.Saldo - dTLotes;

            txtTLotes.Text = dTLotes.ToString();
            txtSinLote.Text = dSinAsignar.ToString();
            txtSaldo.Text = stock.Saldo.ToString();
            
           
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion
}
